<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .list {
            width: 200px;
            height: 150px;
            background-color: skyblue;
            overflow: auto;
        }
        .news {
            height: 30px;
        }
    </style>
</head>
<body>

    <div id="app">
        
    </div>

    <script type="text/javascript" src="../js/17/react.development.js"></script>
    <script type="text/javascript" src="../js/17/react-dom.development.js"></script>
    <script type="text/javascript" src="../js/17/babel.min.js"></script>
    <link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon">
    
    <script type="text/babel">
        class NewsList extends React.Component{
            state = {newsArr: []}
                
            componentDidMount(){
                setInterval(() => {
                    let {newsArr} = this.state
                    let news = '新闻' + (newsArr.length + 1)
                    this.setState({newsArr: [news, ...newsArr]})
                }, 1000)
            }

            getSnapshotBeforeUpdate() {
                return this.refs.list.scrollHeight
            }

            componentDidUpdate(prevProp, preState, height) {
                this.refs.list.scrollTop += this.refs.list.scrollHeight - height
            }

            render() {
                return (
                    <div className="list" ref="list">
                        {
                            this.state.newsArr.map((n, index) => {
                                return <div className="news" key={index}>{n}</div>
                            })
                        }
                    </div>
                )
            }
        }
        ReactDOM.render(<NewsList/>, document.getElementById("app"))
    </script>
</body>
</html>